from backend.app.crud.crud_goods import GoodsDao
from backend.app.services.db_common import DbPinecone
from backend.app.script.db_data_source import DataSourceByExcel


async def insert_by_file(path: str, start_page: int = 1, page_size=100):
    """
    根据文件路径插入excel数据
    :param path:
    :param start_page: 开始页码
    :param page_size: 页码大小
    :return:
    """
    # 初始化数据库
    index_name = "avl"
    db = DbPinecone(index_name=index_name)

    #  读取数据
    td = DataSourceByExcel()
    df = await td.read_excel(path)
    #  获取要插入的数据
    pages = td.get_pages(df.shape[0], page_size=page_size)
    #  分页插入数据
    err_texts, err_metadata = [], []
    for page in range(start_page, pages + 1):
        print(f"[insert_by_file] 总共{pages}页， 第{page} 开始获取当页数据.....")
        texts, metadata = await td.get_meta_data(df, page=page, page_size=page_size)
        print(f"[insert_by_file] 总共{pages}页， 第{page} 完成数据获取.....")
        is_success, result = insert_value(db, texts, metadata)
        # TODO： 更新产品的向量ID到关系型数据库中
        if is_success:
            #  更新向量ID
            vector_id_map = result["vector_id_map"]
            for goods_id, vector_id in vector_id_map.items():
                r = await GoodsDao.update_vector_id(goods_id, vector_id)
            print(f"[insert_by_file] 总共{pages}页， 第{page} 页数据插入完成....")
        else:
            err_texts.extend(result["err_text"])
            err_metadata.extend(result["err_metadata"])
            print(f"[insert_by_file] 总共{pages}页， 第{page} 页数据插入失败....")
    print("页面大小：", page_size)
    print(err_texts)
    print(err_metadata)
    return


def insert_value(db, texts, metadata):
    """插入重试"""
    is_success = True
    result = dict(vector_ids=[], err_text=[], err_metadata=[], vector_id_map=dict())
    if not (texts and metadata):
        return is_success, result

    try:
        result["vector_ids"] = db.batch_add(texts=texts, metadata=metadata)
    except Exception as e:
        print(e)
    vector_id_map = dict()
    for index, vid in enumerate(result["vector_ids"] or []):
        md = metadata[index]
        vector_id_map[md["g_id"]] = vid
    result["vector_id_map"] = vector_id_map
    return is_success, result
